package com.dongdongshop.service.imp;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.dongdongshop.pojo.TbItem;
import com.dongdongshop.pojo.TbOrderItem;
import com.dongdongshop.pojo.TbOrderItemExample;
import com.dongdongshop.service.CartService;
import com.dongdongshop.service.ItemService;
import com.dongdongshop.vo.Cart;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Service
@org.springframework.stereotype.Service
public class CartServiceImp implements CartService {

    @Reference
    private ItemService itemService;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public List<Cart> addCart(List<Cart> cartList, Long itemId, Integer num) {
        //现根据itemId查询商家信息item对象
        TbItem item = itemService.getItemId(itemId);
        //获取商家id
        String sellerId = item.getSellerId();
        //1.如果没有原来商家购物车数据:创建购物车


        Cart cart = getCartBySellerId(sellerId, cartList);
        if (cart == null) {
            cart = new Cart();
            cart.setSellerId(sellerId);

            TbOrderItem orderItem = creatrOrderItem(num, item, itemId);
            List<TbOrderItem> orderItemList = new ArrayList<>();
            orderItemList.add(orderItem);
            cart.setOrderItemList(orderItemList);
            cartList.add(cart);
        } else { //2.如果有原商家购物车信息,但是没有买过这件商品
            TbOrderItem orderItem = getOrderItemByItemId(cart.getOrderItemList(), itemId);
            if (orderItem == null) {
                orderItem = creatrOrderItem(num, item, itemId);
                cart.getOrderItemList().add(orderItem);
            } else { //3.有商家购物车信息且买过这件商品
                orderItem.setNum(orderItem.getNum() + num);//修改数量
                orderItem.setTotalFee(new BigDecimal(orderItem.getPrice().doubleValue() * orderItem.getNum()));
                if (orderItem.getNum() <= 0) {
                    //减完了
                    cart.getOrderItemList().remove(orderItem);//如果这个商品的数量都不需要了,那么就不需要这个商品了
                }
                if (cart.getOrderItemList().size() <= 0) {
                    //如果购物明细中刚好就一个了,然后我们又把他移除了,那么这个明细就没有了
                    //那我们就需要将整个购物车从大的列表中移除
                    cartList.remove(cart);
                }
            }
        }
        return cartList;
    }


    private Cart getCartBySellerId(String sellerId, List<Cart> cartList) {
        for (Cart cart : cartList) {
            if (sellerId.equals(cart.getSellerId())) {
                return cart;
            }
        }
        return null;
    }

    private TbOrderItem creatrOrderItem(Integer num, TbItem item, Long itemId) {
        TbOrderItem orderItem = new TbOrderItem();
        orderItem.setNum(num);
        orderItem.setTitle(item.getTitle());
        orderItem.setPrice(item.getPrice());
        BigDecimal bigDecimal = new BigDecimal(orderItem.getPrice().doubleValue() * num);// BigDecimal//以decimal输出 doubleValue//强转为double
        orderItem.setTotalFee(bigDecimal);
        orderItem.setPicPath(item.getImage());
        orderItem.setGoodsId(item.getGoodsId());
        orderItem.setItemId(itemId);
        orderItem.setSellerId(item.getSellerId());
        return orderItem;
    }

    private TbOrderItem getOrderItemByItemId(List<TbOrderItem> orderItemList, Long itemId) {
        for (TbOrderItem orderItem : orderItemList) {
            if (itemId.equals(orderItem.getItemId())) {
                return orderItem;
            }
        }
        return null;
    }

    @Override
    public List<Cart> findCartListFromRed(String userName) {
        List<Cart> cartList = (List<Cart>) redisTemplate.boundHashOps("cartList").get(userName);
        return cartList == null ? new ArrayList<>():cartList;//防止空指针
    }

    @Override
    public void addCartToRedis(List<Cart> cartList, String userName) {
         redisTemplate.boundHashOps("cartList").put(userName,cartList);
    }

    @Override
    public List<Cart> merageCart(List<Cart> cartListRedis, List<Cart> cartListCookie) {
        for (Cart cart : cartListCookie) {
            String sellerId = cart.getSellerId();//商家id
            List<TbOrderItem> orderItemList = cart.getOrderItemList();
            for (TbOrderItem tbOrderItem : orderItemList) {
                cartListRedis  = addCart(cartListRedis, tbOrderItem.getItemId(), tbOrderItem.getNum());
            }
        }
        return cartListRedis;
    }

}
